<template>
  <el-dialog :visible.sync="show" title="分配角色" @close="closeEvent">
    <el-checkbox
      v-for="item in list"
      :key="item.id"
      v-model="roleArr"
      :label="item.id"
    >{{ item.name }}</el-checkbox>

    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button>取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysRole } from '@/api/setting'
import { sysUser } from '@/api/user'
import { sysUserAssignRoles } from '@/api/employees'
export default {
  data() {
    return {
      show: false,
      roleArr: [],
      list: [],
      id: ''
    }
  },
  methods: {
    async getRoleList() {
      if (this.list.length === 0) {
        const res = await sysRole({
          page: 1,
          pageSize: 9999999
        })
        this.list = res.data.rows
      }
    },
    // 获取用户角色数据
    async getUserRole(id) {
      this.id = id
      const res = await sysUser(id)
      this.roleArr = res.data.roleIds
    },
    closeEvent() {
      this.roleArr = []
    },
    async submit() {
      await sysUserAssignRoles({
        roleIds: this.roleArr,
        id: this.id
      })
      this.$message.success('分配角色成功')
      this.show = false
    }
  }
}
</script>

<style></style>
